﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2022 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using System.Collections.Generic;

namespace Abc.Base;

/// <summary>
/// MVC登陆扩展
/// </summary>
public static class MvcLoginExtensions
{
    /// <summary>
    /// 登陆跳转地址验证
    /// </summary>
    /// <param name="returnUrl"></param>
    /// <param name="defaultReturnUrl">如果验证失败，默认的跳转地址</param>
    /// <returns></returns>
    public static string LoginReturnUrlVer(this string returnUrl, string defaultReturnUrl)
    {
        var domain = App.HttpContext.GetDomain().TrimEnd("/").ToLower();
        //跳转地址为空
        if (string.IsNullOrWhiteSpace(returnUrl))
        {
            if (defaultReturnUrl.ToLower().StartsWith(domain))
            {
                return defaultReturnUrl;
            }
            else
            {
                return domain.ToLower() + "/" + defaultReturnUrl.TrimStart("/");
            }
        }
        else
        {
            //跳转地址不包含当前域名
            if (returnUrl.ToLower().StartsWith(domain))
            {
                return returnUrl;
            }
            else
            {
                //使用默认地址
                if (defaultReturnUrl.ToLower().StartsWith(domain))
                {
                    return defaultReturnUrl;
                }
                else
                {
                    return domain.ToLower() + "/" + defaultReturnUrl.TrimStart("/");
                }
            }
        }
    }

    /// <summary>
    /// 获取MVC登陆安全验证配置
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static MvcLoginSafeVerItem GetMvcLoginSafeVerConfig(string code)
    {
        var mvcLoginSafeVerOptions = App.GetOptionsMonitor<MvcLoginSafeVerOptions>();
        var config = mvcLoginSafeVerOptions.configs.FirstOrDefault(o => o.Code.Trim().ToLower() == code.Trim().ToLower());
        if (config == null) config = new MvcLoginSafeVerItem();
        return config;
    }


    /// <summary>
    /// 获取登陆用户ID
    /// </summary>
    /// <param name="httpContext"></param>
    /// <returns></returns>
    public static int GetLoginUserId(this HttpContext httpContext)
    {
        return httpContext.GetLoginUserId<int>();
    }

    /// <summary>
    /// 获取用户 Id
    /// </summary>
    public static T GetLoginUserId<T>(this HttpContext httpContext)
    {
        if (httpContext == null || httpContext.User == null || httpContext?.User?.Identity?.IsAuthenticated == false) return default;
        if (httpContext?.User?.Claims?.Any(o => o.Type == ClaimConst.Claim_UserId)??false)
        {
            var v = httpContext?.User?.FindFirst(ClaimConst.Claim_UserId)?.Value;
            return v.To<T>();
        }
        return default;
    }

    /// <summary>
    /// 获取用户 UserNick
    /// </summary>
    public static string GetLoginUserNick(this HttpContext httpContext)
    {
        if (httpContext == null || httpContext.User == null || httpContext.User.Identity.IsAuthenticated == false) return "";
        if (httpContext.User.Claims.Any(o => o.Type == ClaimConst.Claim_Name))
        {
            var v = httpContext?.User?.FindFirst(ClaimConst.Claim_Name)?.Value;
            return v.To<string>();
        }
        return httpContext.GetLoginUserAccount();
    }

    /// <summary>
    /// 获取用户 账户
    /// </summary>
    public static string GetLoginUserAccount(this HttpContext httpContext)
    {
        //return httpContext.User.Identity.Name;
        return httpContext?.GetValue<string>(ClaimConst.Claim_Account)??"";
    }

    /// <summary>
    /// 用获取值
    /// </summary>
    /// <param name="httpContext"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetValue(this HttpContext httpContext, string key)
    {
        return httpContext.GetValue<string>(key) ?? "";
    }

    /// <summary>
    ///  用获取值
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="httpContext"></param>
    /// <param name="key"></param>
    /// <returns>返回 TEntity</returns>
    public static TEntity? GetValue<TEntity>(this HttpContext httpContext, string key)
    {
        if (httpContext == null || httpContext.User == null) return default(TEntity);
        if (httpContext.User.FindFirst(key) == null) return default(TEntity);
        var claim = httpContext.User.FindFirst(key);
        if (claim == null || string.IsNullOrEmpty(claim.Value)) return default(TEntity);
        return claim.Value.To<TEntity>();
    }
}